package com.SpringMVC.controller.annotation;

import com.SpringMVC.PropertyEditor.UserPropertyEditor;
import com.SpringMVC.vo.Bank;
import com.SpringMVC.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 使用annotation方式来设置Controller
 */
@Controller
@RequestMapping("/annotation")
@SessionAttributes("user")//实验证明，加上这个属性后，ModelAttribute每次请求会查询Session中是否有该命名对象如果存在则使用session中的命名对象，不进行新的ModelAttribute设置
public class TestAnnotationHandler {
    private static final Logger logger = Logger.getLogger(TestAnnotationHandler.class);

    @RequestMapping(value = "/test0")
    public String test0() {
        System.out.println("123");
        return "success";
    }

    @RequestMapping(value = "/testcallable")
    public Callable<String> testcallable() {
        return new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(3000);
                return "success";
            }
        };
    }

    @RequestMapping(value = "/test1/{id}", method = RequestMethod.GET, params = "create=123", headers = "abc=1")
    public String test1(@PathVariable Integer id) {
        System.out.println("id = [" + id + "]");
        return "success";
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2(@RequestParam(value = "role") List<String> role, @RequestHeader(value = "test", required = false) String test, @CookieValue(value = "coo", required = false) String coo) {
        System.out.println("role = [" + role.size() + "]");
        return "success";
    }

    @RequestMapping(value = "/test3/{id}")
    public String test3(@ModelAttribute User user) {
        System.out.println("userID = [" + user.getId() + "]");
        return "success";
    }

    @RequestMapping("test4")
    public String test4(HttpSession session, Model model, ModelMap modelMap, Map map) {
        if (session.getId() == null) {
            System.out.println("ete");
        } else {
            System.out.println(session.getId());
        }
        model.addAttribute("name", "mike");
        modelMap.put("age", 12);
        map.put("sex", 1);
        return "success";
    }

    @RequestMapping("test5")
    public String test5(User user) {
        System.out.println(user);
        user.setUsername("aaaaa");
        return "success";
    }

    @RequestMapping("test6")
    public String test6(HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println(user == null ? "" : user.getUsername());
        return "success";
    }

//    @ModelAttribute(value = "user")
//    public User getUser(@RequestParam(required = false) Integer id, @RequestParam(required = false) String username) {
//        logger.info("set modelattribute....");
//        User user = new User();
//        user.setId(id);
//        user.setUsername(username);
//        return user;
//    }

    @RequestMapping("test7")
    public String test7(Bank bank) {
        logger.info(bank.getCardNo());
        logger.info(bank.getUser().getId()+":"+bank.getUser().getUsername());
        return "success";
    }

    @RequestMapping("test8")
    public String test8(String userId,String userName) {
        logger.info(userId);
        logger.info(userName);
        return "success";
    }
}
